﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UI
{
   
    public partial class FrmTreeView : Form
    {

        public event TreeNodeMouseClickEventHandler NodeMouseDoubleClick;

        public FrmTreeView(string text)
        {
            InitializeComponent();

            this.Text = text;
        }
        public TreeNodeCollection Nodes
        {
            get
            {
                return this.treeView.Nodes;
            }
        }
        
        

        List<int> RankedNodes = new List<int>();

        public Bitmap NodeRect(Font MyFont, System.Drawing.Color myColor, string RefString = "----")
        {
            SizeF stringSize = new SizeF();
            stringSize = this.CreateGraphics().MeasureString(RefString, MyFont);

            Bitmap flag = new Bitmap((int)stringSize.Width, (int)stringSize.Height);

            float margin = 0.2F;

            Rectangle rect = new Rectangle((int)(margin * flag.Size.Width), (int)(margin * flag.Size.Height), (int)((1F - margin) * flag.Size.Width), (int)((1F - margin) * flag.Size.Height));
 

            Graphics.FromImage(flag).FillRectangle(new System.Drawing.SolidBrush(myColor), rect);

            Graphics.FromImage(flag).DrawRectangle(new Pen(System.Drawing.Color.Black, 2), rect);

            return flag;
        }
        TreeNode AddImage(TreeNode node, Image image)
        {
            if (image != null)
            {
                imageList1.Images.Add(image);

                node.ImageIndex = node.SelectedImageIndex = treeView.ImageList.Images.Count - 1;
            }
            else node.ImageIndex = -1;

            return node;
        }

        public void AddNode(int ranknumber, string text, string ToolTipText, string NoteTag, Image image)
        {
             
            TreeNode node = new TreeNode(text);
            node.Tag = NoteTag;
            node.ToolTipText = ToolTipText;
            node = AddImage(node, image);
             
            int index = 0;
            for (index = 0; index < RankedNodes.Count; index++)
            {
                if (RankedNodes[index] > ranknumber)
                {
                    break;
                }
            }
            this.treeView.Nodes.Insert(index, node);
            RankedNodes.Insert(index, ranknumber);
             
        }
        public bool ChangeNodeText(string old_text, string new_text)
        {
            foreach (TreeNode t in treeView.Nodes)
            {
                if (t.Text == old_text)
                {
                    t.Text = new_text;
                    return true;
                }
            }
            return false;
        }
        public bool ContainsText(string text)
        {
            foreach(TreeNode t in treeView.Nodes)
            {
                if (t.Text == text) return true;
            }
            return false;
        }

        public void ClearNodes()
        {
            this.treeView.Nodes.Clear();
        }
        public bool CheckBoxes
        {
            get
            {
                return treeView.CheckBoxes;
            }
            set
            {
                treeView.CheckBoxes = value;
            }

        }

        public event EventHandler<TreeViewEventArgs> CheckBoxUpdate;

        /// <summary>
        /// Handle a check checkbox on a node
        /// <param name="e"></param>
        private void treeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (this.CheckBoxUpdate != null)
            {
                this.CheckBoxUpdate(new object(), e);
            }
        }

        public void AddContextMenuItem(string label, EventHandler e)
        {
            contextMenuStrip1.Items.Add(label);

            contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Click += e;

        }
        

        private void treeView_MouseClick(object sender, MouseEventArgs e)
        {
            // Pop up menus
            if (e.Button == MouseButtons.Right)
            {
                // Select the clicked node
                treeView.SelectedNode = treeView.GetNodeAt(e.X, e.Y);

                if (treeView.SelectedNode != null)
                {
                    contextMenuStrip1.Show(treeView, e.Location);
                }
                 
            }
            else if (e.Button == MouseButtons.Left)
            { 
                


            }
           
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            double t = 0.0;
        }

       

        private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            // Change ColorScheme
            if (NodeMouseDoubleClick != null)
            {
                if (this.NodeMouseDoubleClick != null)
                {
                    this.NodeMouseDoubleClick(sender, e);
                }
            }

        }

        private void treeView_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
        {
            /*
            toolTip1.RemoveAll();

            TreeNode selNode = (TreeNode)treeView.GetNodeAt(Cursor.Position);

            if (selNode != null)
            {
                if (selNode.Tag != null)
                {
                    toolTip1.SetToolTip(treeView, selNode.Tag.ToString());
                }
                     
            }
             */
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        

        
        
    }
}
